<template>
  <view class="login-container">
    <!-- 提示登陆的图标 -->
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!--登陆按钮 -->
    <button type="primary" class="btn-login" @click="wxLogin">一键登陆</button>
    <!-- 登陆提示 -->
    <view class="tips-text">登陆后先手更多权益</view>
  </view>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  methods: {
    ...mapMutations("m_user", ["updateUserInfo", "updateToken"]),

    async wxLogin() {
      const [err, succ] = await uni.getUserProfile({
        desc: "为了更好的给您提供服务~~",
      });
      if (err === null && succ.errMsg === "getUserProfile:ok") {
        this.updateUserInfo(succ.userInfo);
        this.getToken(succ);
      }
    },

    async getToken(info) {
      const [err, succ] = await uni.login();
      if (err === null && succ.errMsg === "login:ok") {
        const data = {
          code: succ.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature,
        };

        const res = await uni.$request({
          url: "users/wxlogin",
          method: "POST",
          data,
        });

        if (res.meta.status === 200) {
          uni.showToast({
            title: "登陆成功~",
            icon: "none",
            duration: 1000,
          });
          this.updateToken(res.message.token);
          this.navigateBack()
        }
      }
    },
    navigateBack(){
      if (this.getRedirectInfo && this.getRedirectInfo.openType === 'switchTab') {
        uni.switchTab({
          url:this.getRedirectInfo.from,
          complete:()=>{
            this.updateRedirectInfo(null)
          }
        })
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.login-container{
  height: 750rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;
  &::after{
    content: '';
    display: block;
    position: absolute;
    width: 100%;
    height: 40px;
    left: 0;
    bottom: 0;
    background-color: white;
    border-radius: 100%;
    transform: translateY(50%);
  }
  .btn-login{
    width: 90%;
    border-radius: 100px;
    margin: 15px 0;
    background-color: #c00000;
    color: white;
  }
  .tips-text{
    font-size: 12px;
    color: gray;
  }
}
</style>